package com.llkj.qingnianguoxue.mine.mycourse;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.jm.call.CallActivity;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.adapter.MyCourseAdapter;
import com.llkj.qingnianguoxue.base.BaseFragment;
import com.llkj.qingnianguoxue.base.ConstantUserInfo;
import com.llkj.qingnianguoxue.base.MyApplication;
import com.llkj.qingnianguoxue.bean.MyCourseBean;
import com.llkj.qingnianguoxue.bean.PreviousActionBean;
import com.llkj.qingnianguoxue.bean.response.ResponseList;
import com.llkj.qingnianguoxue.course.CourseDetailActivity;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.music.PublicCommentActivity;
import com.llkj.qingnianguoxue.util.GsonUtil;
import com.llkj.qingnianguoxue.util.helper.LoadDataHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.jm.call.JMRtcClientManager.CALL_VIDEO;

/**
 * 我的课程
 */
public class MyCourseFragment extends BaseFragment implements BaseQuickAdapter.RequestLoadMoreListener,
        LoadDataHelper.LoadMoreListener, SwipeRefreshLayout.OnRefreshListener{
    private static final String TAG_STATE = "tag_state";

    @BindView(R.id.refresh_layout)
    SwipeRefreshLayout refreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView lvCourse;
    private int mTagState;
    private int totalSize, mPage, mCurrentPosition;
    private MyCourseAdapter mCourseAdapter;
    private List<MyCourseBean> mCourseData;

    /**
     * 区分是哪个标签
     *
     * @param param1 区分那个tab.
     * @return A new instance of fragment MyCourseFragment.
     */
    public static MyCourseFragment newInstance(int param1) {
        MyCourseFragment fragment = new MyCourseFragment();
        Bundle args = new Bundle();
        args.putInt(TAG_STATE, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mTagState = getArguments().getInt(TAG_STATE);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_my_course, container, false);
        ButterKnife.bind(this,rootView);

        initView();
        initListener();

        onRefresh();
        return rootView;
    }

    private void initView(){
        refreshLayout.setColorSchemeResources(R.color.blue3989ff);
        lvCourse.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        mCourseData = new ArrayList<>();
        mCourseAdapter = new MyCourseAdapter(R.layout.item_my_course,mCourseData);
        mCourseAdapter.setTabType(mTagState);
        lvCourse.setAdapter(mCourseAdapter);
    }

    private void initListener(){
        refreshLayout.setOnRefreshListener(this);
        mCourseAdapter.setOnLoadMoreListener(this);
        lvCourse.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                MyCourseBean courseBean = mCourseData.get(i);
                if(view.getId()==R.id.tv_start_praise){//评价
//                    PublicCommentActivity.startActivity(getActivity(),courseBean.getId());
                }else if(view.getId()==R.id.tv_scan_again){//回看

                }else if(view.getId()==R.id.btn_course_state){//
                    courseDetailActivity(courseBean);
//                    Intent intent = new Intent(getActivity(),CallActivity.class);
//                    intent.putExtra(CALL_VIDEO,"video");
//                    String chatId = "qcgx_"+courseBean.getUid();
//                    intent.putExtra("ChatId",chatId);
//                    startActivity(intent);
                }
            }
        });
        lvCourse.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                MyCourseBean courseBean = mCourseData.get(i);
                courseDetailActivity(courseBean);
            }
        });
    }

    private void courseDetailActivity(MyCourseBean courseBean){
        Bundle bundle = new Bundle();
        String identity = MyApplication.getInstance().sputil.getString(ConstantUserInfo.USER_IDENTITY_TYPE);
        //1 用户 2 老师
        if("1".equals(identity)){
            bundle.putString("id",courseBean.getLive_id());
        }else{
            bundle.putString("id",courseBean.getId());
        }

        bundle.putString("title",courseBean.getName());
        bundle.putInt("type",1);
        startActivity(CourseDetailActivity.class,bundle);
    }

    @Override
    public void onLoadMore() {
        mPage++;
        getCourseData();
    }

    @Override
    public void onRefresh() {
        mPage = 1;
        getCourseData();
    }

    @Override
    public void onLoadMoreRequested() {
        LoadDataHelper.loadMoreRequest(this, mCourseAdapter, null, lvCourse, totalSize);
    }

    private void getCourseData(){
        String identity = MyApplication.getInstance().sputil.getString(ConstantUserInfo.USER_IDENTITY_TYPE);
        //1 用户 2 老师
        String state = null;
        if("2".equals(identity)){
            state = String.valueOf(mTagState-1);
        }else{
            state = String.valueOf(mTagState+1);
        }
        HttpMethod.myCurriculum(getActivity(),this,state,String.valueOf(mPage));
    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);

        if (method == HttpMethod.HTTP_USER_MY_CURRICULUM) {
            ResponseList<MyCourseBean> bean = GsonUtil.fromJsonList(data,MyCourseBean.class);
            if(bean!=null){
                try {
                    totalSize = Integer.parseInt(bean.getNum());
                }catch (Exception e){}
                ;
                if (mPage == 1) {
                    mCourseData.clear();
                    LoadDataHelper.refreshComplete(mCourseAdapter, mCourseData, refreshLayout, lvCourse);
                } else {
                    mCourseAdapter.setEnableLoadMore(true);
                    mCourseAdapter.loadMoreComplete();
                }
                if(bean.getCurriculum()!=null){
                    mCourseData.addAll(bean.getCurriculum());
                }
                mCourseAdapter.notifyDataSetChanged();
            }
        }
    }
}
